﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.DataProvider;
using Project_KhuyenMaiVang.Models;
using System.Data.SqlClient;
using System.Data;

namespace Project_KhuyenMaiVang.DAO
{
    public class TinKhuyenMai_SanPham_HinhAnhDAO : MyDataProvider
    {

        public List<TinKhuyenMai_SanPham_HinhAnhDTO> GetListHottestTKM_SP_HA(int soluong)
        {
            try
            {
                List<TinKhuyenMai_SanPham_HinhAnhDTO> result = new List<TinKhuyenMai_SanPham_HinhAnhDTO>();

                string query = "select top " + soluong.ToString() + " * "
                + "from TinKhuyenMai tkm, SanPham sp "
                + "where tkm.TinhTrang = 'True' and tkm.MaSanPham = sp.MaSanPham and datediff(d, getdate(), tkm.NgayKetThuc) >= 0 " 
                + "order by tkm.SoLuongMua desc ";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TinKhuyenMai_SanPham_HinhAnhDTO ob = (TinKhuyenMai_SanPham_HinhAnhDTO)GetDataFromDataRow(dt, i);
                    ob.ListHinhAnh = this.GetListHinhAnh(ob);
                    result.Add(ob);
                }

                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }

        }

        /// <summary>
        /// Lấy 15 tin khuyến mãi mới nhất bao gồm thông tin của:
        ///     + tin khuyến mãi
        ///     + sản phẩm trong tin khuyến mãi
        ///     + danh sách hình ảnh DTO
        /// </summary>
		/// <param name="MaKhuVuc">int</param>
        /// <returns>list TinKhuyenMai_SanPham_HinhAnhDTO</returns>
        public List<TinKhuyenMai_SanPham_HinhAnhDTO> GetListNewestTKM_SP_HA(int MaKhuVuc)
        {
            try
            {
                List<TinKhuyenMai_SanPham_HinhAnhDTO> result = new List<TinKhuyenMai_SanPham_HinhAnhDTO>();

                string query = "select top 15 * "
                + "from TinKhuyenMai tkm, SanPham sp "
                + "where tkm.TinhTrang = 'True' and tkm.MaSanPham = sp.MaSanPham and tkm.MaKhuVuc = " + MaKhuVuc.ToString()
                + " order by MaTinKhuyenMai desc ";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TinKhuyenMai_SanPham_HinhAnhDTO ob = (TinKhuyenMai_SanPham_HinhAnhDTO)GetDataFromDataRow(dt, i);
                    ob.ListHinhAnh = this.GetListHinhAnh(ob);
                    result.Add(ob);
                }

                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
		
		/// <summary>
        /// Lấy 15 tin khuyến mãi mới nhất bao gồm thông tin của:
        ///     + tin khuyến mãi
        ///     + sản phẩm trong tin khuyến mãi
        ///     + danh sách hình ảnh DTO
        /// </summary>
        /// <returns>list TinKhuyenMai_SanPham_HinhAnhDTO</returns>
        public List<TinKhuyenMai_SanPham_HinhAnhDTO> GetListNewestTKM_SP_HA()
        {
            try
            {
                List<TinKhuyenMai_SanPham_HinhAnhDTO> result = new List<TinKhuyenMai_SanPham_HinhAnhDTO>();

                string query = "select top 15 * "
                + "from TinKhuyenMai tkm, SanPham sp "
                + "where tkm.TinhTrang = 'True' and tkm.MaSanPham = sp.MaSanPham "
                + "order by MaTinKhuyenMai desc ";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TinKhuyenMai_SanPham_HinhAnhDTO ob = (TinKhuyenMai_SanPham_HinhAnhDTO)GetDataFromDataRow(dt, i);
                    ob.ListHinhAnh = this.GetListHinhAnh(ob);
                    result.Add(ob);
                }

                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// Lấy list hình ảnh DTO từ TinKhuyenMai_SanPham_HinhAnhDTO
        /// </summary>
        /// <param name="ob">TinKhuyenMai_SanPham_HinhAnhDTO</param>
        /// <returns>list hình ảnh DTO</returns>
        private List<HinhAnhDTO> GetListHinhAnh(TinKhuyenMai_SanPham_HinhAnhDTO ob)
        {
            try
            {
                List<HinhAnhDTO> result = new List<HinhAnhDTO>();
                HinhAnhDAO helper = new HinhAnhDAO();
                result = helper.GetListHinhAnhBy(ob.MaSanPham);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        public TinKhuyenMai_SanPham_HinhAnhDTO ThongTinChiTietTinKhuyenMai(int IdTin)
        {
            try
            {
                List<TinKhuyenMai_SanPham_HinhAnhDTO> result = new List<TinKhuyenMai_SanPham_HinhAnhDTO>();

                string query = "select * "
                               + "from TinKhuyenMai tkm, SanPham sp "
                               + "where tkm.TinhTrang = 'True' and tkm.MaSanPham = sp.MaSanPham and" 
                               + " tkm.MaTinKhuyenMai = " + IdTin.ToString();           
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TinKhuyenMai_SanPham_HinhAnhDTO ob = (TinKhuyenMai_SanPham_HinhAnhDTO)GetDataFromDataRow(dt, i);
                    ob.ListHinhAnh = this.GetListHinhAnh(ob);
                    result.Add(ob);
                }

                return result.ElementAt(0);
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            TinKhuyenMai_SanPham_HinhAnhDTO TKMSPHADTO = new TinKhuyenMai_SanPham_HinhAnhDTO();
            TKMSPHADTO.MaTinKhuyenMai = Int32.Parse(dt.Rows[i]["MaTinKhuyenMai"].ToString());

            TKMSPHADTO.MaSanPham = Int32.Parse(dt.Rows[i]["MaSanPham"].ToString());
            TKMSPHADTO.TenSanPham = dt.Rows[i]["TenSanPham"].ToString();
            TKMSPHADTO.MaLoaiSanPham = Int32.Parse(dt.Rows[i]["MaLoaiSanPham"].ToString());
            TKMSPHADTO.MoTaChiTiet = dt.Rows[i]["MoTaChiTiet"].ToString();
            TKMSPHADTO.GiaGoc = float.Parse(dt.Rows[i]["GiaGoc"].ToString());
            
            TKMSPHADTO.ThongTinTomTat = dt.Rows[i]["ThongTinTomTat"].ToString();
            TKMSPHADTO.GiaKhuyenMai = float.Parse(dt.Rows[i]["GiaKhuyenMai"].ToString());
            TKMSPHADTO.NgayBatDau = DateTime.Parse(dt.Rows[i]["NgayBatDau"].ToString());
            TKMSPHADTO.NgayKetThuc = DateTime.Parse(dt.Rows[i]["NgayKetThuc"].ToString());
            TKMSPHADTO.SoLuongKhuyenMai = Int32.Parse(dt.Rows[i]["SoLuongKhuyenMai"].ToString());
            TKMSPHADTO.SoLuongMua = Int32.Parse(dt.Rows[i]["SoLuongMua"].ToString());
            TKMSPHADTO.DiemNoiBat = dt.Rows[i]["DiemNoiBat"].ToString();
            TKMSPHADTO.DieuKienSuDung = dt.Rows[i]["DieuKienSuDung"].ToString();
            TKMSPHADTO.IDAccount = Int32.Parse(dt.Rows[i]["IDAccount"].ToString());
            TKMSPHADTO.SoLuongToiThieu = Int32.Parse(dt.Rows[i]["SoLuongToiThieu"].ToString());
            
            return (object)TKMSPHADTO;
        }
    }
}